package cn.fws.meap.community.web;

import java.math.BigDecimal;
import java.util.List;

import cn.fws.framework.utils.StringUtils;

import cn.fws.meap.community.enums.AnswerIsAdoptEnum;
import cn.fws.meap.community.vo.AnswerListVo;
import cn.fws.meap.community.vo.Details;
import cn.fws.meap.user.entity.AccountRecord;
import cn.fws.meap.user.enums.AccountRecordTypeEnums;
import cn.fws.meap.user.enums.ConsumptionEnums;
import cn.fws.meap.user.service.IAccountRecordService;
import cn.fws.meap.user.service.IAccountService;
import cn.fws.meap.utils.logBug.SysLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import cn.fws.meap.community.entity.Answer;
import cn.fws.meap.community.service.IAnswerService;
import cn.fws.framework.web.contoller.BaseController;
import cn.fws.framework.web.page.PagingResult;
import cn.fws.framework.web.contoller.JsonResponse;

/**
 * 回答 信息操作处理
 *
 * @author chenkh
 * @date 2019-03-07
 */
@RestController
@RequestMapping("/meap/answer")
@Api(tags = "回答 信息操作处理")
public class AnswerController extends BaseController {


    @Autowired
    private IAnswerService answerService;
    @Autowired
    private IAccountService accountService;
    @Autowired
    private IAccountRecordService accountRecordService;

    private Boolean flag;

    /**
     * 获取一级问答
     *
     * @param answerId
     * @param openId
     * @return
     */
    @ApiOperation(value = "获取回答列表")
    @PostMapping("/getAnswer")
    public PagingResult<AnswerListVo> getAnswer(String answerId, String openId) {
        flag = false;

        startPage();
        List<AnswerListVo> answerListVo = answerService.getAnswer(answerId, openId, flag);

        return getPagingResult(answerListVo);
    }

    /**
     * 获取二级问答
     *
     * @param answerId
     * @param openId
     * @return
     */
    @ApiOperation(value = "获取二级回答列表")
    @PostMapping("/getTwoAnswer")
    public PagingResult<AnswerListVo> getTwoAnswer(String answerId, String openId) {
        flag = true;
        startPage();
        List<AnswerListVo> answerListVo = answerService.getAnswer(answerId, openId, flag);
        return getPagingResult(answerListVo);
    }

    /**
     * 查询回答列表
     */
    //@RequiresPermissions("meap:answer:list")
    @ApiOperation(value = "查询回答列表", notes = "不用传参")
    @PostMapping("/list")
    public PagingResult<Details<Answer>> list(Answer answer) {
        startPage();
        List<Details<Answer>> list = answerService.selectList(answer);
        return getPagingResult(list);
    }

    /**
     * 新增保存回答
     */
    //@RequiresPermissions("meap:answer:add")
    @ApiOperation(value = "新增保存回答")
    @ApiImplicitParam(name = "answer", value = "新增保存回答", dataType = "Answer")
    @PostMapping("/add")
    public JsonResponse addSave(Answer answer) {
        if (StringUtils.isEmpty(answer.getUserImg())) {
            return error("404", "问题头像不能为空");
        }
        if (StringUtils.isEmpty(answer.getAnswerMsg())) {
            return error("404", "问题内容不能为空");
        }
        if (StringUtils.isEmpty(answer.getSuperId())) {
            return error("404", "父级id不能为空");
        }
        if (StringUtils.isEmpty(answer.getProblemId())) {
            return error("404", "问题id不能为空");
        }
        if (StringUtils.isEmpty(answer.getOpenid())) {
            return error("404", "当前用户id不能为空");
        }


        return toAjax(1, "操作成功", answerService.insert(answer));
    }


    /**
     * 修改保存回答
     */
    @ApiOperation("修改保存回答")
    @ApiImplicitParam(name = "answer", value = "修改保存回答", dataType = "Answer")
    //@RequiresPermissions("meap:answer:edit")
    @PutMapping("/edit")
    public JsonResponse editSave(Answer answer) {
        return toAjax(answerService.update(answer));
    }

    /**
     * 采纳回答
     */
    @SysLog
    @ApiOperation("采纳回答")
    @ApiImplicitParam(value = "采纳回答", dataType = "String")
    //@RequiresPermissions("meap:answer:edit")
    @PutMapping("/adopt")
    public JsonResponse adopt(String answerId, String problemId, String openId, Integer money) {
        if (StringUtils.isEmpty(answerId)) {
            return toAjax(0, "404", "回答ID不能为空");
        }
        if (StringUtils.isEmpty(problemId)) {
            return toAjax(0, "404", "问题ID不能为空");
        }
        if (StringUtils.isEmpty(openId)) {
            return toAjax(0, "404", "OPENID不能为空");
        }


        //查询是否有别的回答被采纳过
        Boolean flag = answerService.selectByAdopt(problemId, AnswerIsAdoptEnum.ADOPT.getCode());

        if (flag) {
            return JsonResponse.error("该问答已被采纳", flag);
        }
        int adopt = answerService.adopt(answerId, problemId, openId, money);
        if (adopt == 0) {
            return toAjax(0, "404", "该问题已经有被采纳的回答");

        }
        flag = false;
        AccountRecord accountRecord = new AccountRecord();
        accountRecord.setOpenid(openId);
        accountRecord.setType(AccountRecordTypeEnums.ANSWER_CONSUMPTION.getCode());
        accountRecord.setConsumption(ConsumptionEnums.INCOME.getCode());
        accountRecord.setAmount(new BigDecimal(money));
        System.out.println("采纳金额——————" + money);
        System.out.println("转换后的金额——————" + accountRecord.getAmount());
        int accountRecordInsert = accountRecordService.insert(accountRecord, flag);

        if (accountRecordInsert > 0) {
            return JsonResponse.success("成功被采纳", accountRecordInsert);
        } else {
            return toAjax(0, "404", "采纳失败");
        }


    }

    /**
     * 删除回答
     */
    //@RequiresPermissions("meap:answer:remove")
    @ApiOperation("删除回答")
    @ApiImplicitParam(name = "ids", value = "根据id删除回答", dataType = "String")
    @DeleteMapping("/remove")
    public JsonResponse remove(String ids) {
        return toAjax(answerService.deleteByIds(ids));
    }

}
